package Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie1")
public class cookie1Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = getCookie(request);
        if(cookie==null){
            response.getWriter().write("您好，您是第一次访问");
        }else{
            System.out.println(cookie.getPath());
            System.out.println(cookie.getValue());
            System.out.println(cookie.getComment());
            System.out.println(cookie.getDomain());
        }
    }
    public Cookie getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();

        if(cookies!=null && cookies.length>0){
           for(Cookie cookie:cookies){
               if("hehe".equals(cookie.getValue())){
                   return cookie;
               }
           }
        }
        return null;
    }
}
